home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cocktail / cg.lha / cg / src / opt.puma < prev    next >
Text File  |  1992-11-24  |  12KB  |  404 lines

  1. /* Ich, Doktor Josef Grosch, Informatiker, 23.5.1989 */
  2.  
  3. TRAFO Optimize
  4. TREE Tree
  5. PUBLIC LifeTime1 LifeTime3
  6.  
  7. EXPORT {
  8. VAR
  9.    ChildrenDyn    ,
  10.    ChildrenIn    ,
  11.    AttributeIn    ,
  12.    AttributeOut    ,
  13.    AttributeTree,
  14.    AttributeParam,
  15.    AttributeVar    ,
  16.    AttributeDemand,
  17.    AttributeStack: SHORTCARD;
  18. }
  19.  
  20. GLOBAL {
  21.  
  22. FROM SYSTEM    IMPORT TSIZE;
  23. FROM General    IMPORT Max;
  24. FROM DynArray    IMPORT MakeArray, ReleaseArray;
  25. FROM IO        IMPORT StdOutput, WriteI, WriteS, WriteNl;
  26. FROM Idents    IMPORT WriteIdent;
  27.  
  28. FROM Sets    IMPORT
  29.    tSet        , IsElement    , AssignElmt    , Union        ,
  30.    Assign    , Minimum    , Maximum    , Include    ;
  31.  
  32. FROM Relations    IMPORT
  33.    IsRelated    ;
  34.  
  35. FROM Tree    IMPORT
  36.    NoTree    , tTree        , tInstance    ,
  37.    Computed    , Reverse    , Write        , Read        ,
  38.    Inherited    , Synthesized    , Input        , Output    ,
  39.    Stack    , Parameter    , Variable    ,
  40.    CopyDef    , CopyUse    , Thread    , Test        ,
  41.    Left        , Right        , Def        , Use        ,
  42.    ChildUse    , ParentUse    , NonBaseComp    , First        ,
  43.    Dummy    , Virtual    , Demand    , f        ,
  44.    WriteName    , Options    , ForallClasses    , ForallAttributes;
  45.  
  46. FROM Order    IMPORT WriteOrderEval, IndexToClass;
  47.  
  48. TYPE tLife    = RECORD Birth, Death: SHORTCARD; END;
  49.  
  50. VAR
  51.    Children    ,
  52.    Parents    ,
  53.    Relevant    : tSet;
  54.    ClassIndex    ,
  55.    MaxChildUse    ,
  56.    MaxParentUse    ,
  57.    Attr        ,
  58.    Comp        ,
  59.    Last        ,
  60.    ChildsVisit    ,
  61.    i, i2, j, j2, k, Visit, v    : SHORTCARD;
  62.    ActChild    ,
  63.    ActClass    ,
  64.    ChildsClass    : tTree;
  65.    LifeSize    : LONGINT;
  66.    LifePtr    : POINTER TO ARRAY [0 .. 10000] OF tLife;
  67. }
  68.  
  69. PROCEDURE LifeTime1 (t: Tree)
  70.  
  71. Class (..) :- {
  72. # ifdef Debug
  73. WriteNl (StdOutput); WriteIdent (StdOutput, Name); WriteNl (StdOutput);
  74. # endif
  75.     LifeSize := InstCount + 1;
  76.     MakeArray (LifePtr, LifeSize, TSIZE (tLife));
  77.     FOR i := 1 TO InstCount DO
  78.        LifePtr^ [i].Birth := 0;
  79.        LifePtr^ [i].Death := 0;
  80.     END;
  81.     Visit := 1;
  82.     FOR i := 1 TO InstCount DO
  83.        i2 := Instance^ [i].Order;
  84.        WITH Instance^ [i2] DO
  85.           IF {Left, Inherited, First} <= Properties THEN
  86.          Visit := Attribute^.Child.Partition;
  87. # ifdef Debug
  88. WriteName (Instance^[i2]); WriteS (StdOutput, "    VISIT    ");
  89. WriteI (StdOutput, Visit, 5); WriteNl (StdOutput);
  90. # endif
  91.           END;
  92.           IF {Left, Synthesized} <= Properties THEN
  93.          LifePtr^ [i2].Birth := Visit;
  94.          INCL (Attribute^.Child.Usage, Visit);
  95. # ifdef Debug
  96. WriteName (Instance^[i2]); WriteS (StdOutput, "    BIRTHl    ");
  97. WriteI (StdOutput, Visit, 5); WriteNl (StdOutput);
  98. # endif
  99.           END;
  100.           IF Right IN Properties THEN
  101.          LifePtr^ [i2].Birth := Visit;
  102. # ifdef Debug
  103. WriteName (Instance^[i2]); WriteS (StdOutput, "    BIRTHr    ");
  104. WriteI (StdOutput, Visit, 5); WriteNl (StdOutput);
  105. # endif
  106.           END;
  107.           IF ({Left, Synthesized, First} <= Properties) AND NOT (Dummy IN Properties) OR
  108.          ({Right,  Inherited, First} <= Properties) THEN
  109.          FOR j := 1 TO AttrCount DO
  110.             IF IsRelated (i2, j, DP) THEN
  111.                LifePtr^ [j].Death := Visit;
  112.                INCL (Instance^ [j].Attribute^.Child.Usage, Visit);
  113. # ifdef Debug
  114. WriteName (Instance^[j]); WriteS (StdOutput, "    DEATHl    ");
  115. WriteI (StdOutput, Visit, 5); WriteNl (StdOutput);
  116. # endif
  117.             END;
  118.          END;
  119.          FOR j := AttrCount + 1 TO InstCount DO
  120.             IF IsRelated (i2, j, DP) THEN
  121.                LifePtr^ [j].Death := Visit;
  122. # ifdef Debug
  123. WriteName (Instance^[j]); WriteS (StdOutput, "    DEATHr    ");
  124. WriteI (StdOutput, Visit, 5); WriteNl (StdOutput);
  125. # endif
  126.             END;
  127.          END;
  128.           END;
  129.           IF {Right, Synthesized, First} <= Properties THEN
  130.          ActClass := t;
  131.          ActChild := Selector;
  132.          ChildsClass := Selector^.Child.Class;
  133.          ChildsVisit := Attribute^.Child.Partition;
  134.          LifeTime2 (ChildsClass);
  135.          ForallClasses (ChildsClass^.Class.Extensions, LifeTime2);
  136.           END;
  137.        END;
  138.     END;
  139.     FOR i := 1 TO AttrCount DO
  140.        WITH Instance^ [i] DO
  141.           IF (Synthesized IN Properties) AND (LifePtr^ [i].Birth < LifePtr^ [i].Death) THEN
  142.          INCL (Attribute^.Child.Properties, Tree.Tree);
  143.           END;
  144.        END;
  145. # ifdef Debug
  146. WriteName (Instance^[i]); WriteS (StdOutput, "    LIFEl    ");
  147. WriteI (StdOutput, LifePtr^[i].Birth, 5); WriteI (StdOutput, LifePtr^[i].Death, 5); WriteNl (StdOutput);
  148. # endif
  149.     END;
  150.     FOR i := AttrCount + 1 TO InstCount DO
  151.        IF LifePtr^ [i].Birth < LifePtr^ [i].Death THEN
  152.           WITH Instance^ [i] DO
  153.          INCL (Attribute^.Child.Properties, Tree.Tree);
  154.           END;
  155.        END;
  156. # ifdef Debug
  157. WriteName (Instance^[i]); WriteS (StdOutput, "    LIFEr    ");
  158. WriteI (StdOutput, LifePtr^[i].Birth, 5); WriteI (StdOutput, LifePtr^[i].Death, 5); WriteNl (StdOutput);
  159. # endif
  160.     END;
  161.     ReleaseArray (LifePtr, LifeSize, TSIZE (tLife));
  162. }; .
  163.  
  164.  
  165. PROCEDURE LifeTime2 (t: Tree)
  166.  
  167. Class (..) :- {
  168.     v := 1;
  169.     FOR j := 1 TO InstCount DO
  170.        j2 := Instance^ [j].Order;
  171.        WITH Instance^ [j2] DO
  172.           IF {Left, Inherited, First} <= Properties THEN
  173.          v := Attribute^.Child.Partition;
  174. # ifdef Debug
  175. WriteIdent (StdOutput, Name); WriteS (StdOutput, "    ");
  176. WriteName (Instance^[j2]); WriteS (StdOutput, "    VISIT2    ");
  177. WriteI (StdOutput, v, 5); WriteNl (StdOutput);
  178. # endif
  179.          IF v > ChildsVisit THEN RETURN; END;
  180.           END;
  181.           IF NOT (Dummy IN Properties) AND (ChildsVisit = v) THEN
  182.          FOR k := 1 TO ChildsClass^.Class.AttrCount DO
  183.             IF IsRelated (j2, k, DP) THEN
  184.                LifePtr^ [ActClass^.Class.AttrCount + ActChild^.Child.InstOffset + k].Death := Visit;
  185. # ifdef Debug
  186. WriteIdent (StdOutput, Name); WriteS (StdOutput, "    ");
  187. WriteName (Instance^[j2]); WriteS (StdOutput, "    ");
  188. WriteName (Instance^[k]); WriteS (StdOutput, "    ");
  189. WriteName (ActClass^.Class.Instance^[ActClass^.Class.AttrCount + ActChild^.Child.InstOffset + k]);
  190. WriteS (StdOutput, "    DEATH    "); WriteI (StdOutput, Visit, 5); WriteNl (StdOutput);
  191. # endif
  192.             END;
  193.          END;
  194.           END;
  195.        END;
  196.     END;
  197. }; .
  198.  
  199.  
  200. PROCEDURE LifeTime3 (t: Tree)
  201.  
  202. Class (..) :- {
  203.     ActClass := t;
  204.     ForallAttributes (Attributes, LifeTime3);
  205. }; .
  206. Child (..) :- {
  207.     INCL (Properties, Tree.Tree);
  208.     IF Input IN Properties THEN
  209.        INC (ChildrenIn);
  210.     ELSE
  211.        INC (ChildrenDyn);
  212.     END;
  213. }; .
  214. Attribute (..) :- {
  215.       IF IsElement (ORD ('0'), Options) THEN
  216.     IF (Input IN Properties) OR (Output IN Properties) THEN
  217.        INCL (Properties, Tree.Tree);
  218.        IF Input IN Properties THEN
  219.           INC (AttributeIn);
  220.        ELSE
  221.           INC (AttributeOut);
  222.        END;
  223.     END;
  224.     IF NOT (Tree.Tree IN Properties) THEN
  225.        INCL (Properties, Parameter);
  226.     END;
  227.       ELSE
  228.     INCL (Properties, Tree.Tree);
  229.       END;
  230.     IF {Test, Dummy, Virtual, Demand} * Properties # {} THEN
  231.        EXCL (Properties, Tree.Tree);
  232.        EXCL (Properties, Parameter);
  233.     END;
  234.       IF IsElement (ORD ('3'), Options) THEN
  235.     IF ({Test, Dummy, Virtual, Input, Output} * Properties) = {} THEN
  236.        WriteIdent    (StdOutput, ActClass^.Class.Name);
  237.        WriteS    (StdOutput, "    = ");
  238.        WriteIdent    (StdOutput, Name);
  239.        WriteS    (StdOutput, "    ");
  240.        IF Tree.Tree IN Properties THEN
  241.           WriteS    (StdOutput, "Tree"    ); INC (AttributeTree);
  242.        ELSIF Parameter IN Properties THEN
  243.           WriteS    (StdOutput, "Parameter"    ); INC (AttributeParam);
  244.        ELSIF Stack IN Properties THEN
  245.           WriteS    (StdOutput, "Stack"    ); INC (AttributeStack);
  246.        ELSIF Variable IN Properties THEN
  247.           WriteS    (StdOutput, "Variable"    ); INC (AttributeVar);
  248.        ELSIF Demand IN Properties THEN
  249.           WriteS    (StdOutput, "Demand"    ); INC (AttributeDemand);
  250.        END;
  251.        WriteNl    (StdOutput);
  252.     END;
  253.       END;
  254. }; .
  255.  
  256.  
  257. /*
  258. PROCEDURE LifeTime4 (t: Tree)
  259.  
  260. Class (..) :- {
  261.     AssignElmt (Children, Index);
  262.     Assign (Parents, Users);
  263.     ForallClasses (Extensions, LifeTime5);
  264.     ForallAttributes (Attributes, LifeTime4);
  265. }; .
  266. Attribute (..) :- {
  267.     IF ({Test, Dummy, Virtual, Input, Output} * Properties) = {} THEN
  268. WriteS (StdOutput, "attr = "); WriteIdent (StdOutput, Name); WriteNl (StdOutput);
  269.        MaxChildUse := 0;
  270.        FOR ClassIndex := Minimum (Children) TO Maximum (Children) DO
  271.           IF IsElement (ClassIndex, Children) THEN
  272.          ActClass := IndexToClass^[ClassIndex];
  273.          WITH ActClass^.Class.Instance^ [AttrIndex] DO
  274.             IF Synthesized IN Properties THEN INCL (Properties, Def); END;
  275.          END;
  276.          Last := 0;
  277.          Visit := 1;
  278.          WITH ActClass^.Class DO
  279.             FOR i := 1 TO InstCount DO
  280.                i2 := Instance^ [i].Order;
  281.                WITH Instance^ [i2] DO
  282.               IF {Left, Inherited, First} <= Properties THEN
  283.                  Visit := Attribute^.Child.Partition;
  284.               END;
  285.               IF (({Left, Synthesized, First} <= Properties) OR
  286.                  ({Right, Inherited, First} <= Properties)) AND
  287.                  NOT (Dummy IN Properties) AND IsRelated (i2, AttrIndex, DP) THEN
  288.                  IF ({ChildUse, ParentUse} * Properties) # {} THEN 
  289. WriteS (StdOutput, "multiple use = "); WriteI (StdOutput, i2, 0); WriteNl (StdOutput);
  290. WriteI (StdOutput, MaxChildUse, 0); WriteNl (StdOutput);
  291. WriteOrderEval (ActClass);
  292.                 RETURN;
  293.                  END;
  294.                  INCL (Properties, ChildUse);
  295.                  IF Last # 0 THEN EXCL (Instance^ [Last].Properties, ChildUse); END;
  296.                  Last := i2;
  297.                  MaxChildUse := Max (MaxChildUse, Visit);
  298.               END;
  299.                END;
  300.             END;
  301.          END;
  302.           END;
  303.        END;
  304.  
  305.        Attr := AttrIndex;
  306.        MaxParentUse := 0;
  307.        FOR ClassIndex := Minimum (Parents) TO Maximum (Parents) DO
  308.           IF IsElement (ClassIndex, Parents) THEN
  309.          ActClass := IndexToClass^[ClassIndex];
  310.          ForallAttributes (ActClass, LifeTime5);
  311.           END;
  312.        END;
  313.  
  314. WriteS (StdOutput, "MaxChildUse  = "); WriteI (StdOutput, MaxChildUse, 0); WriteNl (StdOutput);
  315. WriteS (StdOutput, "MaxParentUse = "); WriteI (StdOutput, MaxParentUse, 0); WriteNl (StdOutput);
  316.  
  317.        IF MaxParentUse >= MaxChildUse THEN
  318.           FOR ClassIndex := Minimum (Children) TO Maximum (Children) DO
  319.          IF IsElement (ClassIndex, Children) THEN
  320.             ActClass := IndexToClass^[ClassIndex];
  321.             WITH ActClass^.Class DO
  322.                FOR i := 1 TO InstCount DO
  323.               WITH Instance^ [i] DO
  324.                  EXCL (Properties, ChildUse);
  325.               END;
  326.                END;
  327.             END;
  328.          END;
  329.           END;
  330.        ELSE
  331.           FOR ClassIndex := Minimum (Parents) TO Maximum (Parents) DO
  332.          IF IsElement (ClassIndex, Parents) THEN
  333.             ActClass := IndexToClass^[ClassIndex];
  334.             WITH ActClass^.Class DO
  335.                FOR i := 1 TO InstCount DO
  336.               WITH Instance^ [i] DO
  337.                  EXCL (Properties, ParentUse);
  338.               END;
  339.                END;
  340.             END;
  341.          END;
  342.           END;
  343.        END;
  344.  
  345.        Assign (Relevant, Children);
  346.        Union (Relevant, Parents);
  347.        FOR ClassIndex := Minimum (Relevant) TO Maximum (Relevant) DO
  348.           IF IsElement (ClassIndex, Relevant) THEN
  349.          ActClass := IndexToClass^[ClassIndex];
  350. WriteOrderEval (ActClass);
  351.          WITH ActClass^.Class DO
  352.             FOR i := 1 TO InstCount DO
  353.                WITH Instance^[i] DO
  354.               Properties := Properties - {Def, Use, ChildUse, ParentUse};
  355.                END;
  356.             END;
  357.          END;
  358.           END;
  359.        END;
  360.     END;
  361. }; .
  362.  
  363.  
  364. PROCEDURE LifeTime5 (t: Tree)
  365.  
  366. Class (..) :- {
  367.     Include (Children, Index);
  368.     Union (Parents, Users);
  369. }; .
  370. Child (..) :- {
  371.     Comp := ActClass^.Class.AttrCount + InstOffset + Attr;
  372.     Last := 0;
  373.     WITH ActClass^.Class.Instance^ [Comp] DO
  374.        IF Inherited IN Properties THEN INCL (Properties, Def); END;
  375.     END;
  376.     Visit := 0;
  377.     WITH ActClass^.Class DO
  378.        FOR i := 1 TO InstCount DO
  379.           i2 := Instance^ [i].Order;
  380.           WITH Instance^ [i2] DO
  381.          IF ({Right, Synthesized, First} <= Properties) AND (Selector = t) THEN
  382.             Visit := Attribute^.Child.Partition;
  383.          END;
  384.          IF (({Left, Synthesized, First} <= Properties) OR
  385.             ({Right, Inherited, First} <= Properties)) AND
  386.             NOT (Dummy IN Properties) AND IsRelated (i2, Comp, DP) THEN
  387.             IF ({ChildUse, ParentUse} * Properties) # {} THEN 
  388. WriteS (StdOutput, "multiple use = "); WriteI (StdOutput, i2, 0); WriteNl (StdOutput);
  389. WriteI (StdOutput, MaxChildUse, 0); WriteNl (StdOutput);
  390. WriteOrderEval (ActClass);
  391.                RETURN;
  392.             END;
  393.             INCL (Properties, ParentUse);
  394.             IF Last # 0 THEN EXCL (Instance^ [Last].Properties, ParentUse); END;
  395.             Last := i2;
  396.             MaxParentUse := Max (MaxParentUse, Visit);
  397.          END;
  398.           END;
  399.        END;
  400.     END;
  401. }; .
  402.  
  403. */
  404.